       
var theusername = sessionStorage.getItem("name");
var theuserid = sessionStorage.getItem("userId");

var sendData = {};  
if(sessionStorage.getItem("name")){ 
	$('#name').text(sessionStorage.getItem("name"));	
	sendData.jobnumber =sessionStorage.getItem("jobnumber");
	sendData.userId =sessionStorage.getItem("userId");	
	onload();
}else{
	var _config ;
	$.ajax({
		type : "post",
		data:JSON.stringify({"pageUrl":location.href.replace(/\&/g,'%26')}),
		contentType: "application/json; charset=utf-8",
		async: false,
		url : "../dingding/getconfig",
		dataType : "json",
		success : function(suc) {
			_config = suc.config;
		}
	}); 
//	dd.config({
//		agentId : _config.agentid,
//		corpId : _config.corpId,
//		timeStamp : _config.timeStamp,
//		nonceStr : _config.nonceStr,
//		signature : _config.signature,
//		jsApiList : [ 'runtime.info', 'biz.contact.choose',
//				'device.notification.confirm', 'device.notification.alert',
//				'device.notification.prompt', 'biz.ding.post',
//				'biz.util.openLink' ]
//	});
	dd.ready(function() {
		//channel runtime  requestOperateAuthCode requestAuthCode
	    dd.runtime.permission.requestAuthCode({
	        corpId: _config.corpId,
	        onSuccess: function(result) {
	        	getuserId(result);               
//	         
	        },
	        onFail : function(err) {
	        	alert("错误"+JSON.stringify(err))
	        }
	 
	    });
	});
	function getuserId(result){
		 //$("body").text('https://oapi.dingtalk.com/user/getuserinfo?code=' + result.code + '&access_token='+ _config.token);
		$.ajax({
			url : '../dingding/dd',
			type : 'POST',
			data:JSON.stringify({"code":result.code,"token":_config.token}),
			contentType: "application/json; charset=utf-8",
			dataType : "json",
			success : function(suc) {
			    if(suc.status == '1002'){
			    	sessionStorage.setItem("userId",suc.userJson.userid);
			    	sessionStorage.setItem("name",suc.userJson.name);
			    	sessionStorage.setItem("jobnumber",suc.userJson.jobnumber);
			    	sessionStorage.setItem("deptId",suc.userJson.department);
			    	$('#name').text(sessionStorage.getItem("name"));
					sendData.planuserid =sessionStorage.getItem("userId");
					setthenum();
			    	onload();
			    }else{
			    	alert(suc.msg);
			    }

			},
			error : function(xhr, errorType, error) {
					alert(errorType + ', ' + error);
				}
		});
	}
	dd.error(function(err) {
		alert('dd error: ' + JSON.stringify(err));
	});
}



function onload(){
    var shop=sessionStorage.getItem("shopcode");
    if (shop)
    {
    $('#shop').text(sessionStorage.getItem("shopcode")+'.'+sessionStorage.getItem("shopname"));}
	else 
	{
	 history.go(-1);
	}
	$('#goodsno').focus();
	$('#saveButton').on('click',saveInfo);
};

function getRequest(name) {
   var url=window.location.search;  
   var theRequest= new Object;
   if (url.indexOf("?")!=-1)
   {
      var str=url.substr(1);
      strs=str.split("&");
      for(var i=0;i<strs.length;i++)
      {
       theRequest[strs[i].split("=")[0]]=decodeURI(strs[i].split("=")[1]);
      }      
   }
   return theRequest;
   
}; 
$('#searchButton').on('click',function(){
var  goodsno=$('#goodsno').val();
var sendData = {};
sendData.goodsno=goodsno;
	$.ajax({
		url: "../../busswork/getGoodsInfo"
		,type:'post'
		,dataType:'json'
		,contentType: "application/json; charset=utf-8"
		,data:JSON.stringify(sendData) 
		,success: function(res){
		var  tbhtml=$('#data tbody').html();
		if (res.data.length==0)
		{
	    	layer.msg("商品码错误，请重新输入！");
		}		
		for(var p in res.data){  
			tbhtml=tbhtml+"<tr ><td>"+res.data[p].gdsincode+"</td><td>"+res.data[p].gdsname+"</td><td>1</td><td onclick='deltr(this);' style='color:#F4511E'>删除</td></tr>";			
		}	    
		$('#data tbody').html(tbhtml);		
		$('#goodsno').val("");
		$('#goodsno').focus();
		}
		});
});

function deltr(nowtr)
{
$(nowtr).parent().remove();
}

function saveInfo(){

var sendData = [];
var sd={};
//sd.Deptcode=sessionStorage.getItem("shopcode");
sd.Deptcode="1096";
sd.Group="";
sd.Type="1";

$('#data  tbody tr').each(function(){
    sendData.push( $(this).find('td')[0].innerText);
});
var  goodsno= sendData[0];
sd.Gdscode=goodsno;
console.log(sd);

$.ajax({
url: '../../yaohuo/jiaqiansqSave'
,type:'post'
,dataType:'json'
,contentType: "application/json; charset=utf-8"
,data:JSON.stringify(sd) 
,success: function(res){
console.log(res);
if (res.status == '1') {
layer.msg(res.msg);
} else {
layer.msg(res.msg);
}
}
})	
}
